#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
from _bdtp import bdtp_storage

bdtp_base_url = "https://image.baidu.com/search/acjson?tn=resultjson_com&logid=11310753460261423676&" \
                "ipn=rj&ct=201326592&is=&fp=result&queryWord={0}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&" \
                "st=-1&z=&ic=0&hd=&latest=&copyright=&word={1}&s=&se=&tab=&width=&height=&face=0&istype=2&" \
                "qc=&nc=1&fr=&expermode=&force=&pn={2}&rn={3}&gsm=78&1602480906711="
bdtu_page_count = 4
page_size = 30
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0"}

def bdtp_images(keyword):
    images = []
    urls = [bdtp_base_url.format(keyword, keyword, (i * page_size), page_size) for i in range(1, bdtu_page_count)]
    for url in urls:
        r = requests.get(url, headers=headers)
        r.encoding = r.apparent_encoding
        results = r.json().get("data")
        for result in results:
            images.append(result.get("middleURL"))

    return images

if __name__ == '__main__':
    keyword = "王一博"
    bdtp_storage.storage_local(keyword, bdtp_images(keyword))